package com.sunny.setavatordemo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.sunny.setavatordemo.activity.PicturePickerActivity;
import com.sunny.setavatordemo.uitls.DensityUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private final static int REQ_CODE = 1;

    private List<String> permissions = new ArrayList<String>();
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle("上传图片");

        if (Build.VERSION.SDK_INT >= 23) {

            permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
            permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);

            List<String> deniedPermissions = getDeniedPermissions();

            if (deniedPermissions.size() > 0) {

                requestPermissions(deniedPermissions.toArray(new String[deniedPermissions.size()]), 1);
            }
        }

        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startPickerActivity();
            }
        });
    }

    private List<String> getDeniedPermissions() {

        List<String> deniedPermissions = new ArrayList<>();

        for (String permission : permissions) {

            int grantCode = ActivityCompat.checkSelfPermission(MainActivity.this, permission);

            if (grantCode == PackageManager.PERMISSION_DENIED) {
                deniedPermissions.add(permission);
            }
        }
        return deniedPermissions;
    }

    private void startPickerActivity() {

        Intent intent = new Intent();

        intent.setClass(MainActivity.this, PicturePickerActivity.class);
        startActivityForResult(intent, REQ_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (REQ_CODE == 1) {

            if (resultCode == RESULT_OK) {

                String path = data.getStringExtra("path");

                Picasso.get().load("file://" + path)
                        .config(Bitmap.Config.RGB_565)
                        .centerCrop()
                        .resize(DensityUtil.dip2px(MainActivity.this, 80),
                                DensityUtil.dip2px(MainActivity.this, 80))
                        .into(imageView);
            }

        }
    }
}
